public class Text {

    public int[] searchRange(int[] nums, int target) {
        //利用二分查找去做
        //先处理一下数组为空的情况
        int[] arr = new int[2];
        arr[0] = arr[1] = -1;
        if(nums.length == 0) {
            return arr;
        }
        //开始找左端点
        int left = 0; int right = nums.length - 1;
        while(left < right) {
            int mid = left + (right - left)/2;  //这里不能加一  不然也会出现死循环
            //如果中点下标的值小于目标值
            if(nums[mid] < target) {
                left = mid + 1;
            }else{
                right = mid;   //这里不能小于mid  因为mid下表的值可能就是你要找的目标值
            }
        }
        if(nums[left] != target) return arr;
        else  arr[0] = right;

        left = 0;
        right = nums.length - 1;
        //开始找右端点
        while(left < right) {
            int mid = left + (right - left + 1) / 2;   //这里必须加一 不然剩两个的时候  就会出现死循环
            if(nums[mid] <= target) left = mid;
            else right = mid - 1;
        }
        arr[1] = right;
        return arr;
    }
      //二分查找
    public int search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while(left <= right) {
            int mid = left + (right - left) / 2;  //防溢出
            if(nums[mid] < target ) left = mid + 1;
            else if(nums[mid] > target) right = mid - 1;
            else return mid;
        }
        return -1;
    }
    public static void main(String[] args) {
    }
}
